package com.android.launcher3;

import java.util.ArrayList;

import android.content.ComponentName;
import android.content.ContentValues;
import android.content.Intent;
import android.graphics.Bitmap;

class ShortcutInfo extends ItemInfo {
	Intent.ShortcutIconResource iconResource;
	CharSequence title;
	Intent intent;
	boolean customIcon;
	boolean onExternalStorage;
	boolean usingFallbackIcon;
	int badgeCount;
	private Bitmap mIcon;

	/**
	 * Creates a new ShortcutInfo object.
	 */
	public ShortcutInfo(final ApplicationInfo app) {
		super(app);
		this.title = app.title.toString();
		this.intent = app.intent;
		this.customIcon = false;
		this.badgeCount = app.badgeCount;
	}

	/**
	 * Creates a new ShortcutInfo object.
	 */
	public ShortcutInfo(final ShortcutInfo info) {
		super(info);

		this.title = info.title.toString();
		this.intent = info.intent;

		if (info.iconResource != null) {
			this.iconResource = new Intent.ShortcutIconResource();
			this.iconResource.packageName = info.iconResource.packageName;
			this.iconResource.resourceName = info.iconResource.resourceName;
			;
		}

		this.mIcon = info.mIcon;
		this.customIcon = info.customIcon;
		;
		this.badgeCount = info.badgeCount;
	}

	/**
	 * Creates a new ShortcutInfo object.
	 */
	ShortcutInfo() {
		this.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT;
	}

	public static void dumpShortcutInfoList(final String paramString1,
			final String paramString2,
			final ArrayList<ShortcutInfo> paramArrayList) {
	}

	public Bitmap getIcon(final IconCache iconCache) {
		if (this.mIcon == null) {
			this.mIcon = iconCache.getIcon(this.intent);
		}

		return this.mIcon;
	}

	public void setIcon(final Bitmap bm) {
		this.mIcon = bm;
	}

	public String toString() {
		return new StringBuilder().append("ShortcutInfo(title=")
				.append(this.title).append("").toString();
	}

	void onAddToDatabase(final ContentValues values) {
		super.onAddToDatabase(values);

		String titleStr = title != null ? title.toString() : null;
		values.put(LauncherSettings.BaseLauncherColumns.TITLE, titleStr);

		String uri = intent != null ? intent.toUri(0) : null;
		values.put(LauncherSettings.BaseLauncherColumns.INTENT, uri);

		if (customIcon) {
			values.put(LauncherSettings.BaseLauncherColumns.ICON_TYPE,
					LauncherSettings.BaseLauncherColumns.ICON_TYPE_BITMAP);
			writeBitmap(values, mIcon);
		} else {
			if (!usingFallbackIcon) {
				writeBitmap(values, mIcon);
			}

			values.put(LauncherSettings.BaseLauncherColumns.ICON_TYPE,
					LauncherSettings.BaseLauncherColumns.ICON_TYPE_RESOURCE);

			if (iconResource != null) {
				values.put(LauncherSettings.BaseLauncherColumns.ICON_PACKAGE,
						iconResource.packageName);
				values.put(LauncherSettings.BaseLauncherColumns.ICON_RESOURCE,
						iconResource.resourceName);
			}
		}
	}

	final void setActivity(final ComponentName className, final int launchFlags) {
		intent = new Intent(Intent.ACTION_MAIN);
		intent.addCategory(Intent.CATEGORY_LAUNCHER);
		intent.setComponent(className);
		intent.setFlags(launchFlags);
		itemType = LauncherSettings.BaseLauncherColumns.ITEM_TYPE_APPLICATION;
	}
}
